import {
  columns,
  tabArr
} from './index.const';
import ElEditable from '@components/el-editable';
import {
  fileDownload
} from '@const/utils.js';
import {
  cloneDeep
} from 'lodash';
import tabs from '@components/tabs/index.vue';
import {
  payOrderMainIcbcVerifyManagePageList,
  payOrderMainIcbcVerifyManageExport,
  payOrderMainGetPayeeName,
  payOrderMainGetPayerName
} from '@api/interface_center_v1.js';
import {
  $loadFileSimply
} from '@const/loadFileOps.js';
export default {
  components: {
    ElEditable,
    tabs
  },
  inject: ['apiMMallWeb'],
  data() {
    return {
      searchParams: {
        orderNo: '', // 订单编号
        payeeName: '', // 收款方
        payerName: '', // 付款方
        toAccountDate: [], // 到账时间
        payDate: [] // 支付时间
      },
      payeeNameOptions: [], // 收款方下拉
      payerNameOptions: [], // 付款方下拉
      paySuccessStatus: '', // 已支付状态
      toAccountSuccessStatus: '', // 已到账状态
      excelBtnDisabled: false, // 导出按钮控制
      tableLoading: false,
      tabArr: tabArr,
      columns: columns,
      payload: {},
      // 主列表分页
      currentPage: 1,
      pageSize: 20
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // tab切换
    handleTab(index) {
      this.paySuccessStatus = index === 1 ? 1 : '';
      this.toAccountSuccessStatus = index === 2 ? 1 : '';
      this.search();
    },
    // 重置
    reset() {
      // eslint-disable-next-line guard-for-in
      for (let key in this.searchParams) {
        if (key === 'toAccountDate' || key === 'payDate') {
          this.searchParams[key] = [];
        } else {
          this.searchParams[key] = '';
        }
      }
      this.search();
    },
    // 查询
    search() {
      this.currentPage = 1;
      this.getList();
    },
    // 清除下拉选择
    clearOptions(name) {
      if (!this.searchParams.payeeName) this.payeeNameOptions = [];
      if (!this.searchParams.payerName) this.payerNameOptions = [];
    },
    // 远程搜索 付款方 收款方的下拉
    getPayOptions(name, keyName) {
      let params = {};
      let requestUrl = keyName === 'payeeName' ? payOrderMainGetPayeeName : payOrderMainGetPayerName;
      params[keyName] = name;
      this.$axios({
        method: 'get',
        url: requestUrl,
        params
      }).then(res => {
        if (keyName === 'payeeName') {
          this.payeeNameOptions = res.data.payload || [];
        } else {
          this.payerNameOptions = res.data.payload || [];
        }
      });
    },
    // 查询参数处理
    getSearchParams() {
      let searchParams = cloneDeep(this.searchParams);
      searchParams.toAccountDateStart =
        searchParams.toAccountDate && searchParams.toAccountDate.length ? searchParams.toAccountDate[0] : '';
      searchParams.toAccountDateEnd =
        searchParams.toAccountDate && searchParams.toAccountDate.length ? searchParams.toAccountDate[1] : '';
      searchParams.payDateStart = searchParams.payDate && searchParams.payDate.length ? searchParams.payDate[0] : '';
      searchParams.payDateEnd = searchParams.payDate && searchParams.payDate.length ? searchParams.payDate[1] : '';
      searchParams.paySuccessStatus = this.paySuccessStatus;
      searchParams.toAccountSuccessStatus = this.toAccountSuccessStatus;
      delete searchParams.toAccountDate;
      delete searchParams.payDate;
      return searchParams;
    },
    // 获取列表数据
    getList() {
      this.tableLoading = true;
      let searchParams = this.getSearchParams();
      this.$axios({
          method: 'post',
          url: payOrderMainIcbcVerifyManagePageList,
          data: {
            page: this.currentPage,
            pageSize: this.pageSize,
            ...searchParams
          }
        })
        .then(res => {
          this.payload = res.data.payload;
        })
        .finally(() => {
          this.tableLoading = false;
        });
    },
    // 主列表分页器
    reloadTableData(val) {
      this.currentPage = val.number;
      this.pageSize = val.size;
      this.getList();
    },
    // 导出
    exportEx() {
      let searchParams = this.getSearchParams();
      this.excelBtnDisabled = true;

      $loadFileSimply({
          method: 'post',
          data: searchParams,
          url: payOrderMainIcbcVerifyManageExport
        })
        .then(res => {
          fileDownload(res, '工行对账管理.xlsx');
        })
        .finally(() => {
          this.excelBtnDisabled = false;
        });
    },
    // 查看
    checkDetail(row) {
      // /m-mall-pc/#/financial-management/platform/Icbc-statement-management
      this.$router.push({
        path: '/financial-management/platform/Icbc-statement-management/detail',
        query: {
          id: row.id
        }
      });
    }
  }
};